package surface;

import utils.Vector2d;
import java.util.*;
import java.awt.*;
import java.io.Serializable;

/**
 * A simple class to handle the surface itself. All objects are polylines.
 * 
 * @author L. Simon, Univ. Paris Sud, 2008.
 *
 */
public class Surface implements Serializable {


	public int wxsize, wysize;

	// All objects on the surface are recorded in this vector of polylines.
	private Vector<PolylineObject> objects; // The objects on the surface

	/**
	 * Well, right now the objects are built "by hands". May by the first
	 * thing to do would be to put polylines objects in a map, and read the
	 * file and objects.
	 * 
	 * In this case, the size of the surface should be set inside the constructor?
	 * 
	 * @param wxsize
	 * @param wysize
	 * @param scale
	 */
	public Surface( int wxsize, int wysize, float scale) {
		this.wxsize = wxsize;
		this.wysize = wysize;
		objects = new Vector<PolylineObject>();
	}

	public void addPolylineObject(PolylineObject object) {
		objects.add(object);
	}
	
	public Vector<PolylineObject> getObjects() {
		return objects;
	}
	
	/**
	 * Draws all objects on the surface.
	 * 
	 * @param g
	 */
	public void draw(Graphics g) {
		for(int i=0;i<objects.size();i++) {
			objects.get(i).draw(g);
		}
	}

	/**
	 * One of the main methods. Checks if the segment (tmpA, tmpB)
	 * intersects any of the lines of any polyline. If not, then
	 * the point tmpA 'can see' the point tmpB.
	 * 
	 * @param tmpA
	 * @param tmpB
	 * @return true if tmpA can see tmpB
	 */
	public boolean cansee(Vector2d tmpA, Vector2d tmpB) {
		for(int i=0;i<objects.size();i++) {
			if (objects.get(i).intersectsWith(tmpA, tmpB))
				return false;
		}
		return true;

	}

	public boolean isInSurface(Vector2d point, int radius) {
		return (point.x > radius) && (point.x < wxsize - radius) && (point.y > radius) && (point.y < wysize - radius);
	}
}

